package com.lankystar.logistucs.spring.dao.impl;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.logistucs.spring.dao.ICompanyDao;
import com.lankystar.logistucs.spring.entity.Company;

@Repository
public class CompanyDaoImpl extends BaseDaoImpl implements ICompanyDao {

	@Autowired
	private SessionFactory sf;
	
	public Session getSession(){
		System.out.println("session oneOrderDaoImpl");
		return sf.getCurrentSession();
	}

	@Override
	public Company selectCompany(int userId) {
		String sql = "select cp from User u join u.company cp where u.userId = ?";
		Company company = (Company) getSession().createQuery(sql).setInteger(0, userId).uniqueResult();
		return company;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> selectCompanyFleetlist() {
		String sql = "select cp from User u join u.company cp join u.userType ut where ut.userKey = 101";
		List<Company> companylist = getSession().createQuery(sql).list(); 
		return companylist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> selectCompanyBrokerlist() {
		String sql = "select cp from User u join u.company cp join u.userType ut where ut.userKey = 102";
		List<Company> companylist = getSession().createQuery(sql).list();
		return companylist;
	}

	@Override
	public Company selectCompanyById(int companyId) {
		Company company = (Company) getSession().get(Company.class, companyId);
		return company;
	}
}
